﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.Common;
using BaHuan.BusinessLayer;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmTongHopNhapXuatHang : Form
    {
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private CTPhieuChuyenKhoManagement ctphieuchuyenkhoMgt = new CTPhieuChuyenKhoManagement();
        private CTPhieuHangTraManagement ctphieuhangtraMgt = new CTPhieuHangTraManagement();
        private CTPhieuNhapKhoMgt ctphieunhapkhoMgt = new CTPhieuNhapKhoMgt();

        public FrmTongHopNhapXuatHang()
        {
            InitializeComponent();
        }

        private void FrmTongHopNhapXuatHang_Load(object sender, EventArgs e)
        {
            cboKhoMuaHang.DataSource = GetAllChiNhanhCongTy();
            cboKhoMuaHang.DisplayMember = "TenChiNhanh";
            cboKhoMuaHang.ValueMember = "Id";

            cboKhoNhapHang.DataSource = GetAllChiNhanhCongTy();
            cboKhoNhapHang.DisplayMember = "TenChiNhanh";
            cboKhoNhapHang.ValueMember = "Id";

            cboKhoXuat.DataSource = GetAllChiNhanhCongTy();
            cboKhoXuat.DisplayMember = "TenChiNhanh";
            cboKhoXuat.ValueMember = "Id";

            cboKhoNhan.DataSource = GetAllChiNhanhCongTy();
            cboKhoNhan.DisplayMember = "TenChiNhanh";
            cboKhoNhan.ValueMember = "Id";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (tabTongHop.SelectedIndex == 0)
            {
                DateTime dtTuNgay = dtpTuNgayMua.Value;
                DateTime dtDenNgay = dtpDenNgayMua.Value;

                // Tab Mua nguyen vat lieu
                int iIdChiNhanhCongTy = 0;
                if (cboKhoMuaHang.Text != "")
                {
                    if (((ChiNhanhCongTy)cboKhoMuaHang.SelectedItem).TenChiNhanh != "")
                    {
                        ChiNhanhCongTy cntyKhoMuaHang = (ChiNhanhCongTy)cboKhoMuaHang.SelectedItem;
                        iIdChiNhanhCongTy = cntyKhoMuaHang.Id;
                    }
                }
                else
                {
                    MessageBox.Show("Vui lòng chọn kho mua hàng", "Thông báo");
                    return;
                }

                var lst = (from m in GetTongHopNhapKho()
                           where m.NgayThang.Date >= dtTuNgay.Date && m.NgayThang.Date <= dtDenNgay.Date
                           select m);

                if (iIdChiNhanhCongTy != 0)
                {
                    lst = lst.Where(m => m.IdChiNhanhCTy == iIdChiNhanhCongTy);
                }

                if (lst.ToList().Count == 0)
                {
                    MessageBox.Show("Không có dữ liệu theo yêu cầu !", "Thông báo");
                    return;
                }

                FrmXemReport frmXemReport = new FrmXemReport(lst.ToList(), dtTuNgay, dtDenNgay);
                this.Hide();
                frmXemReport.ShowDialog();
                this.Show();
            }
            else if (tabTongHop.SelectedIndex == 1)
            {
                DateTime dtTuNgay = dtpTuNgayLK.Value;
                DateTime dtDenNgay = dtpDenNgayLK.Value;

                // Tab Liet ke nhap hang tra
                int iIdChiNhanhCongTy = -1;
                if (cboKhoNhapHang.Text != "")
                {
                    if (((ChiNhanhCongTy)cboKhoNhapHang.SelectedItem).TenChiNhanh != "")
                    {
                        ChiNhanhCongTy cntyKhoNhapHang = (ChiNhanhCongTy)cboKhoNhapHang.SelectedItem;
                        iIdChiNhanhCongTy = cntyKhoNhapHang.Id;
                    }
                }
                else
                {
                    MessageBox.Show("Vui lòng chọn kho nhập hàng", "Thông báo");
                    return;
                }

                var lst = (from m in GetTongHopHangTra()
                           where m.NgayLap.Date >= dtTuNgay.Date && m.NgayLap.Date <= dtDenNgay.Date
                           select m);

                if (iIdChiNhanhCongTy != -1)
                {
                    lst = lst.Where(m => m.IdChiNhanhCongTy == iIdChiNhanhCongTy);
                }

                if (lst.ToList().Count == 0)
                {
                    MessageBox.Show("Không có dữ liệu theo yêu cầu !", "Thông báo");
                    return;
                }

                FrmXemReport frmXemReport = new FrmXemReport(lst.ToList(), dtTuNgay, dtDenNgay);
                this.Hide();
                frmXemReport.ShowDialog();
                this.Show();
            }
            else if (tabTongHop.SelectedIndex == 2)
            {
                DateTime dtTuNgay = dtpTuNgay.Value;
                DateTime dtDenNgay = dtpDenNgay.Value;

                // Tab Chuyen kho
                string sKhoXuat = "";
                string sKhoNhan = "";

                if (cboKhoXuat.Text != "")
                {
                    if (((ChiNhanhCongTy)cboKhoXuat.SelectedItem).TenChiNhanh != "")
                    {
                        ChiNhanhCongTy cntyKhoXuat = (ChiNhanhCongTy)cboKhoXuat.SelectedItem;
                        sKhoXuat = cntyKhoXuat.TenChiNhanh;
                    }
                }
                else
                {
                    MessageBox.Show("Vui lòng chọn kho xuất","Thông báo");
                    return;
                }

                if (cboKhoNhan.Text != "")
                {
                    if (((ChiNhanhCongTy)cboKhoNhan.SelectedItem).TenChiNhanh != "")
                    {
                        ChiNhanhCongTy cntyKhoNhan = (ChiNhanhCongTy)cboKhoNhan.SelectedItem;
                        sKhoNhan = cntyKhoNhan.TenChiNhanh;
                    }
                }
                else
                {
                    MessageBox.Show("Vui lòng chọn kho nhận", "Thông báo");
                    return;
                }

                var lst = (from m in GetTongHopChuyenKho()
                           where m.NgayThang.Date >= dtTuNgay.Date && m.NgayThang.Date <= dtDenNgay.Date
                           select m);

                if (!string.IsNullOrEmpty(sKhoXuat))
                {
                    lst = lst.Where(m => m.KhoXuat == sKhoXuat);
                }

                if (!string.IsNullOrEmpty(sKhoNhan))
                {
                    lst = lst.Where(m => m.KhoNhan == sKhoNhan);
                }

                if (lst.ToList().Count < 1)
                {
                    MessageBox.Show("Không có dữ liệu theo yêu cầu !", "Thông báo");
                    return;
                }

                FrmXemReport frmXemReport = new FrmXemReport(lst.ToList(), dtTuNgay, dtDenNgay);
                this.Hide();
                frmXemReport.ShowDialog();
                this.Show();
            }
        }

        private void cboKhoMuaHang_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboKhoMuaHang.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboKhoMuaHang.Text) && cboKhoMuaHang.Text != "")
                {
                    cboKhoMuaHang.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhoMuaHang.SelectedValue == null)
            {
                cboKhoMuaHang.Text = "";
                cboKhoMuaHang.Focus();
            }
        }

        private void cboKhoNhapHang_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboKhoNhapHang.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboKhoNhapHang.Text) && cboKhoNhapHang.Text != "")
                {
                    cboKhoNhapHang.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhoNhapHang.SelectedValue == null)
            {
                cboKhoNhapHang.Text = "";
                cboKhoNhapHang.Focus();
            }
        }

        private void cboKhoXuat_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboKhoXuat.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboKhoXuat.Text) && cboKhoXuat.Text != "")
                {
                    cboKhoXuat.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhoXuat.SelectedValue == null)
            {
                cboKhoXuat.Text = "";
                cboKhoXuat.Focus();
            }
        }

        private void cboKhoNhan_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboKhoNhan.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboKhoNhan.Text) && cboKhoNhan.Text != "")
                {
                    cboKhoNhan.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhoNhan.SelectedValue == null)
            {
                cboKhoNhan.Text = "";
                cboKhoNhan.Focus();
            }
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<TongHopNhapKho> GetTongHopNhapKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = ctphieunhapkhoMgt.GetTongHopNhapKho();
            List<TongHopNhapKho> listTongHopNhapKho = (List<TongHopNhapKho>)resultObj.Result;

            return listTongHopNhapKho;
        }

        public List<TongHopHangTra> GetTongHopHangTra()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = ctphieuhangtraMgt.GetTongHopHangTra();
            List<TongHopHangTra> listTongHopHangTra = (List<TongHopHangTra>)resultObj.Result;

            return listTongHopHangTra;
        }

        public List<TongHopChuyenKho> GetTongHopChuyenKho()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = ctphieuchuyenkhoMgt.GetTongHopChuyenKho();
            List<TongHopChuyenKho> listTongHopChuyenKho = (List<TongHopChuyenKho>)resultObj.Result;

            return listTongHopChuyenKho;
        }
    }
}
